/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.actions;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.JTextComponent;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.opu.db_vdumper.resources.ConfigPath;
import org.opu.db_vdumper.resources.ResourceHandle;
import org.opu.db_vdumper.resources.ResourceHandle.IconType;
import org.opu.db_vdumper.ui.MainView;
import org.opu.db_vdumper.ui.Tetris;
import org.opu.db_vdumper.ui.i18n.AboutI18N;
import org.opu.db_vdumper.ui.i18n.AboutI18NImpl;
import org.opu.db_vdumper.util.ui.GUITextUtils;

/**
 *
 * @author yura
 */
public class About extends AbstractAction {
    /** My name */
    public static final String MNAME = "About";
    public static final String ICON = "aboutDialog.png";
    public static final int ETALON_W = 600;
    public static final int ETALON_H = 300;
    public static final int BORDER = 10;
    private MainView view;
    private AboutI18N i18N;

    public About(MainView view, AboutI18N i18N) {
        this.i18N = i18N;
        this.view = view;
    }

    public About(MainView view) {
        this(view, new AboutI18NImpl());
    }

    @Override
    public String getName() {
        return MNAME;
    }

    @Override
    public void action(ActionEvent e) {
        JFrame frame = view.asFrame();
        final JDialog dialog = new JDialog(frame, frame.getTitle(), true);

        Icon icon = ResourceHandle.getIcon(ICON, IconType.medium);
        final JLabel imgLbl = new JLabel(icon);
        imgLbl.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                int w = imgLbl.getWidth();
                int h = imgLbl.getHeight();
                int x1 = w * 230 / ETALON_W, x2 = w * 350 / ETALON_W,
                        y1 = h * 130 / ETALON_H, y2 = h * 192 / ETALON_H;
                if (e.getButton() == MouseEvent.BUTTON3 && e.getClickCount() == 2){
                    Point p = e.getPoint();
                    if (p.x > x1 && p.x < x2 && p.y > y1 && p.y < y2){
                        System.out.println("EGG");
                        Tetris.run(dialog);
                    }
                }
            }
        });
        
        JTextPane aboutTxt = GUITextUtils.addActions(new JTextPane());
        aboutTxt.setText(i18N.getAbout());

        DefaultStyledDocument doc = new DefaultStyledDocument();
        append(doc, i18N.getVendor(), ResourceHandle.getApplicationVendor() + "\n");
        append(doc, i18N.getVersion(), ResourceHandle.getApplicationVersionString() + "\n");
        append(doc, i18N.getName(), ResourceHandle.getApplicationName() + "\n");
        append(doc, "\n");
        append(doc, i18N.getJava(), System.getProperty("java.version") + "\n");
        append(doc, i18N.getSystem(), System.getProperty("os.name") + " " 
                + System.getProperty("os.version") + "\n");
        append(doc, i18N.getHomeFolder(), System.getProperty("user.home") + "\n");
        append(doc, i18N.getConfigsFolder(), ConfigPath.getInstance()
                .getApplicationConfDir().getAbsolutePath());

        JTextPane confTxt = GUITextUtils.addActions(new JTextPane(doc));
        
        Box bottmBox = Box.createHorizontalBox();
        bottmBox.add(Box.createHorizontalGlue());
        bottmBox.add(new JButton(new javax.swing.AbstractAction(i18N.getClose()) {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        }));
        bottmBox.add(Box.createHorizontalGlue());

        Box vbox = Box.createVerticalBox();
        vbox.add(Box.createVerticalStrut(BORDER));
        vbox.add(compInPane(imgLbl));

        Box hbox = Box.createHorizontalBox();
        hbox.add(Box.createHorizontalStrut(BORDER));
        hbox.add(vbox);
        hbox.add(Box.createHorizontalStrut(BORDER));
        
        dialog.getContentPane().add(hbox);
        dialog.pack();
        vbox.add(Box.createVerticalStrut(BORDER));
        vbox.add(compInPane(aboutTxt));
        vbox.add(Box.createVerticalStrut(BORDER));
        vbox.add(compInPane(confTxt));
        vbox.add(Box.createVerticalStrut(BORDER));
        vbox.add(bottmBox);
        vbox.add(Box.createVerticalStrut(BORDER));
        Dimension size = dialog.getSize();
        size.height += 300;
        dialog.setSize(size);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    private void append(DefaultStyledDocument doc, String name, String val) {
        try {
            SimpleAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setBold(attr, true);
            doc.insertString(doc.getLength(), name + ": ", attr);
            doc.insertString(doc.getLength(), val, new SimpleAttributeSet());
        } catch (BadLocationException ex) {
            throw new RuntimeException(ex);
        }
    }

    private void append(DefaultStyledDocument doc, String str){
        try {
            doc.insertString(doc.getLength(), str, new SimpleAttributeSet());
        } catch (BadLocationException ex) {
            throw new RuntimeException(ex);
        }
    }

    private JPanel compInPane(JComponent comp){
        JPanel pnl = new JPanel(new BorderLayout());
        pnl.add(comp);
        pnl.setBorder(BorderFactory.createLineBorder(Color.GRAY));

        if (comp instanceof JTextComponent){
            JTextComponent tc = (JTextComponent) comp;
            tc.setEditable(false);
            tc.setBackground(UIManager.getColor("panel.color"));
        }

        return pnl;
    }
}
